package svc

import (
	"go-weixin/app/guitar/api/internal/config"
	"go-weixin/app/guitar/rpc/guitarclient"
	"go-weixin/app/usercenter/rpc/userrpcclient"
	"go-weixin/common/apiserver"
	"go-weixin/common/rpcserver"
	"go-weixin/common/utils"

	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config    config.Config
	GuitarRPC guitarclient.Guitar
	UserRpc   userrpcclient.UserCenter
	ApiMDLE   *apiserver.Middleware
	Utils     *utils.Utils
}

func NewServiceContext(c config.Config) *ServiceContext {
	rpcMDLE := rpcserver.NewMiddleware()
	userRpc := userrpcclient.NewUserRpcClient(zrpc.MustNewClient(zrpc.RpcClientConf{Endpoints: c.UserRpc.Endpoints, NonBlock: c.UserRpc.NonBlock}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware)))
	guitarRpc := guitarclient.NewGuitar(zrpc.MustNewClient(zrpc.RpcClientConf{
		Endpoints: c.GuitarRpcConf.Endpoints,
		NonBlock:  c.GuitarRpcConf.NonBlock,
	}, zrpc.WithUnaryClientInterceptor(rpcMDLE.ClientRpcMiddleware)))
	return &ServiceContext{
		Config:    c,
		GuitarRPC: guitarRpc,
		UserRpc:   userRpc,
		ApiMDLE:   apiserver.NewApiMiddleware(userRpc),
		Utils:     utils.UT,
	}
}
